package cn.hpclub.server.plugin;

import java.io.IOException;

import org.nutz.ssdb4j.SSDBs;
import org.nutz.ssdb4j.spi.SSDB;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.IPlugin;

public class SsdbPlugin implements IPlugin{

    private String host;
    private String cacheName;
    private int port;
    private String clientName;
    private int timeout;
    private String password;
    private int database;
    private boolean enabled;

    public SsdbPlugin() {
        this.enabled = false;
    }
    
    public SsdbPlugin(String cacheName, String host) {
        if (StrKit.isBlank(cacheName))
            throw new IllegalArgumentException("cacheName can not be blank.");
        if (StrKit.isBlank(host))
            throw new IllegalArgumentException("host can not be blank.");
        this.cacheName = cacheName.trim();
        this.host = host;
        this.enabled = true;
    }

    public SsdbPlugin(String cacheName, String host, int port) {
        this(cacheName, host);
        this.port = port;
    }
    
    public SsdbPlugin(String cacheName, String host, int port, int timeout) {
        this(cacheName, host, port);
        this.timeout = timeout;
    }
    
    public SsdbPlugin(String cacheName, String host, int port, int timeout, String password) {
        this(cacheName, host, port, timeout);
        if (StrKit.isBlank(password))
            throw new IllegalArgumentException("password can not be blank.");
        this.password = password;
    }
    
    public SsdbPlugin(String cacheName, String host, int port, int timeout, String password, int database) {
        this(cacheName, host, port, timeout, password);
        this.database = database;
    }
    
    public SsdbPlugin(String cacheName, String host, int port, int timeout, String password, int database, String clientName) {
        this(cacheName, host, port, timeout, password, database);
        if (StrKit.isBlank(clientName))
            throw new IllegalArgumentException("clientName can not be blank.");
        this.clientName = clientName;
    }
    
    public SsdbPlugin(String cacheName, String host, int port, String password) {
        this(cacheName, host, port, SSDBs.DEFAULT_TIMEOUT, password);
    }
    
    public SsdbPlugin(String cacheName, String host, String password) {
        this(cacheName, host, SSDBs.DEFAULT_PORT, SSDBs.DEFAULT_TIMEOUT, password);
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
        Ssdb.setEnabled(enabled);
    }
    @Override
    public boolean start(){
        if(!this.enabled) {
            return false;
        }
        SSDB ssdb;
        if(StrKit.notBlank(password)){
            ssdb = SSDBs.pool(host, port, timeout, null,password.getBytes());
        }else{
            ssdb = SSDBs.pool(host, port, timeout, null);
        }
        if(ssdb == null) {
            
            return false;
        }
        Cache cache = new Cache(cacheName, ssdb);
        Ssdb.addCache(cache);
        return true;
    }

    @Override
    public boolean stop(){
        if(!this.enabled) {
            return false;
        }
        Cache cache = Ssdb.removeCache(cacheName);
        if (cache == Ssdb.mainCache)
            Ssdb.mainCache = null;
        try{
            cache.ssdb.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        return true;
    }

}
